﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Types.Interval;
using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the x, y, z values are filtered instead of R G B
    /// </summary>
    public class CIEXYZFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_XInterval = new DoubleInterval(0.0f, 10.0f);
        protected DoubleInterval m_YInterval = new DoubleInterval(0.0f, 10.0f);
        protected DoubleInterval m_ZInterval = new DoubleInterval(0.0f, 10.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public CIEXYZFilter()
        {
        }

        /// <summary>
        /// specify intervals directly
        /// </summary>
        /// <param name="_x"></param>
        /// <param name="_y"></param>
        /// <param name="_z"></param>
        public CIEXYZFilter(DoubleInterval _x, DoubleInterval _y, DoubleInterval _z)
        {
            m_XInterval = _x;
            m_YInterval = _y;
            m_ZInterval = _z;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_ciexyzfilter"></param>
        internal CIEXYZFilter(CIEXYZFilter _ciexyzfilter)
        {
            this.m_XInterval = _ciexyzfilter.m_XInterval;
            this.m_YInterval = _ciexyzfilter.m_YInterval;
            this.m_ZInterval = _ciexyzfilter.m_ZInterval;
        }

        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        public DoubleInterval X
        {
            get { return m_XInterval; }
            set { m_XInterval = value; }
        }

        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        public DoubleInterval Y
        {
            get { return m_YInterval; }
            set { m_YInterval = value; }
        }

        /// <summary>
        /// Gets or sets the Z.
        /// </summary>
        /// <value>The Z.</value>
        public DoubleInterval Z
        {
            get { return m_ZInterval; }
            set { m_ZInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            CIEXYZ xyz = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    xyz = new CIEXYZ(bitmap.GetPixel(x, y));

                    if (m_XInterval.Contains(xyz.X)
                        && m_YInterval.Contains(xyz.Y)
                        && m_ZInterval.Contains(xyz.Z))
                    {
                        bitmap.SetPixel(x, y, xyz.ToColor());
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CIEXYZFilter(this);
        }
    }
}
